package org.gl.training.planner.service;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import org.gl.training.planner.dao.DailyPlanDao;
import org.gl.training.planner.dao.TaskDao;
import org.gl.training.planner.dao.UserDao;
import org.gl.training.planner.model.DailyPlan;
import org.gl.training.planner.model.Task;
import org.gl.training.planner.model.User;
import org.primefaces.event.DateSelectEvent;

@ManagedBean(name = "dailyPlanBean")
@RequestScoped
public class DailyPlanBean {

	@ManagedProperty(value = "#{param.id}")
	private Integer id;

	@ManagedProperty(value = "#{dataHolderBean}")
	private DataHolderBean dataHolderBean;

	@ManagedProperty(value = "#{taskBean}")
	private TaskBean taskBean;

	@EJB
	private UserDao userDao;

	@EJB
	private TaskDao taskDao;

	@EJB
	private DailyPlanDao dailyPlanDao;

	private User user;

	private DailyPlan dailyPlan;

	private String errorMessage;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public void setDailyPlanDao(DailyPlanDao dailyPlanDao) {
		this.dailyPlanDao = dailyPlanDao;
	}

	public void setDataHolderBean(DataHolderBean dataHolderBean) {
		this.dataHolderBean = dataHolderBean;
	}

	public void setTaskDao(TaskDao taskDao) {
		this.taskDao = taskDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public void setTaskBean(TaskBean taskBean) {
		this.taskBean = taskBean;
	}

	public User getUser() {
		if (user == null) {
			initializeUser();
		}
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public DailyPlan getDailyPlan() {
		if (dailyPlan == null) {
			initializeDailyPlan();
		}
		return dailyPlan;
	}

	public void setDailyPlan(DailyPlan dailyPlan) {
		this.dailyPlan = dailyPlan;
	}

	/**
	 * 
	 */
	protected void initializeUser() {
		if (id == null) {
			user = dataHolderBean.getUser();
		} else {
			user = userDao.find(id);
		}
	}

	/**
	 * 
	 */
	protected void initializeDailyPlan() {
		dailyPlan = dailyPlanDao.getDailyPlan(getUser().getId(),
				dataHolderBean.getDate());
		if (dailyPlan == null) {
			dailyPlan = new DailyPlan();
			dailyPlan.setDate(dataHolderBean.getDate());
			dailyPlan.setUser(getUser());
			dailyPlan.setTaskList(new ArrayList<Task>());
		}
	}

	protected void refreshDailyPlan() {
		dailyPlan = null;
	}

	public void handleCalendarChange(DateSelectEvent event) {
		refreshDailyPlan();
		initializeDailyPlan();
	}

	public boolean isTasksFlag() {
		return getDailyPlan() != null && getDailyPlan().getTaskList() != null
				&& getDailyPlan().getTaskList().size() > 0;
	}

	public String delete() {
		List<Task> taskSelectedList = getSelectedTaskList();
		for (Task task : taskSelectedList) {
			taskDao.delete(task);
		}
		refreshDailyPlan();
		return null;
	}

	public String create() {
		Task task = new Task();
		task.setDailyPlan(getDailyPlan());

		taskBean.setTask(task);
		taskBean.setFullName(getUser().getFullName());
		taskBean.setDate(getDailyPlan().getDate());

		refreshDailyPlan();
		return "success";
	}

	public String edit() {
		List<Task> taskSelectedList = getSelectedTaskList();

		if (taskSelectedList.size() == 0) {
			errorMessage = "No task selected";
			return null;
		}
		if (taskSelectedList.size() > 1) {
			errorMessage = "Select only one task";
			return null;
		}
		Task task = taskSelectedList.get(0);
		task.setDailyPlan(getDailyPlan());

		taskBean.setTask(task);
		taskBean.setFullName(getUser().getFullName());
		taskBean.setDate(getDailyPlan().getDate());

		refreshDailyPlan();
		return "success";
	}

	private List<Task> getSelectedTaskList() {
		List<Task> taskSelectedList = new ArrayList<Task>();
		for (Task task : getDailyPlan().getTaskList()) {
			if (task.isSelected()) {
				taskSelectedList.add(task);
			}
		}
		return taskSelectedList;
	}

	public boolean isLoggedInUserFlag() {
		User currentUser = dataHolderBean.getUser();
		User dailyPlanUser = getUser();
		return currentUser.getId().equals(dailyPlanUser.getId());
	}
}
